Conditions | 1 |
Paths | 16 |
Total Lines | 64 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
13 | function(state, util, data) { |
||
14 | // FIXME: move to util? |
||
15 | function isReactionCostMet (number, reaction, playerData) { |
||
16 | for (let key in reaction.reactant) { |
||
17 | let available = playerData.resources[key]; |
||
18 | let required = number * reaction.reactant[key]; |
||
19 | if (required > available) { |
||
20 | return false; |
||
21 | } |
||
22 | } |
||
23 | return true; |
||
24 | } |
||
25 | |||
26 | /* Transforms reactants to products */ |
||
27 | this.react = function(number, reaction, playerData) { |
||
28 | if (!Number.isInteger(number) || number <= 0 || |
||
29 | !reaction.reactant || !reaction.product) { |
||
30 | return; |
||
31 | } |
||
32 | if (isReactionCostMet(number, reaction, playerData)) { |
||
33 | let elements = []; |
||
34 | for (let resource in reaction.reactant) { |
||
35 | let required = number * reaction.reactant[resource]; |
||
36 | playerData.resources[resource] -= required; |
||
37 | playerData.resources[resource] = playerData.resources[resource]; |
||
38 | // We track which elements produced the products, for the statistics |
||
39 | for(let elem of Object.keys(data.resources[resource].elements)){ |
||
40 | if(elements.indexOf(elem) === -1){ |
||
41 | elements.push(elem); |
||
42 | } |
||
43 | } |
||
44 | } |
||
45 | for (let resource in reaction.product) { |
||
46 | let produced = number * reaction.product[resource]; |
||
47 | util.addResource(playerData, elements, resource, produced, state); |
||
48 | } |
||
49 | } |
||
50 | }; |
||
51 | |||
52 | this.processReactions = function(reactions, player){ |
||
53 | let declared = {}; |
||
54 | for(let reaction of reactions){ |
||
55 | let reactant = reaction.reaction.reactant; |
||
56 | for (let resource in reactant) { |
||
57 | declared[resource] = declared[resource]+reactant[resource]*reaction.number || reactant[resource]*reaction.number; |
||
58 | } |
||
59 | } |
||
60 | for(let reaction of reactions){ |
||
61 | let reactant = reaction.reaction.reactant; |
||
62 | for (let resource in reactant) { |
||
63 | if(!declared[resource] || !reactant[resource]){ |
||
64 | continue; |
||
65 | } |
||
66 | let available = Math.min(declared[resource], player.resources[resource]); |
||
67 | let ratio = reactant[resource]*reaction.number/declared[resource]; |
||
68 | reaction.number = Math.min(reaction.number, Math.floor(available*ratio)); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | for(let reaction of reactions){ |
||
73 | this.react(reaction.number, reaction.reaction, player); |
||
74 | } |
||
75 | } |
||
76 | } |
||
77 | ]); |
||
78 |